package com.marc.eden;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import com.marc.eden.util.ConUtil;
import com.marc.eden.util.Util;
import com.megvii.facepp.sdk.Facepp;
import com.megvii.licensemanager.sdk.LicenseManager;

public class LauncherActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);

        network();
    }

    private void network() {
        if (Facepp.getSDKAuthType(ConUtil.getFileContent(this, R.raw
                .megviifacepp_0_4_1_model)) == 2) {
            Log.d("AuthType: ", "非联网授权");
            authState(true);
        }
        //正在联网授权中
        final LicenseManager licenseManager = new LicenseManager(this);
        licenseManager.setExpirationMillis(Facepp.getApiExpirationMillis(this, ConUtil.getFileContent(this, R.raw
                .megviifacepp_0_4_1_model)));

        String uuid = ConUtil.getUUIDString(LauncherActivity.this);
        long[] apiName = {Facepp.getApiName()};

        licenseManager.takeLicenseFromNetwork(uuid, Util.API_KEY, Util.API_SECRET, apiName,
                LicenseManager.DURATION_30DAYS, new LicenseManager.TakeLicenseCallback() {
                    @Override
                    public void onSuccess() {
                        authState(true);
                    }

                    @Override
                    public void onFailed(int i, byte[] bytes) {
                        authState(false);
                    }
                });
    }

    private void authState(boolean isSuccess) {
        if (isSuccess) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        } else {
            Log.d("AuthState: ", "联网授权失败");
            network();
        }
    }

}
